<!DOCTYPE html>
<html lang="en">
    <head>
            <meta charset="utf-8">
            <meta http-equiv="X-UA-Compatible" content="IE=edge">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <meta content="Bmob,BmobCloud,bomb,BaaS,mBaaS,PaaS,Serverless,FaaS,Function as a Service,Backend as a Service,serverless computing,cloud function,后端云,bmob后端云,小程序云,小程序后端,云数据库,云存储,文件存储,云函数,云端代码,定时任务,游戏后端,游戏云,用户系统,无服务器函数,移动开发,app开发,小程序开发,云端一体化,互联网中间件" name="keywords"/>
            <meta content="国内首家专注于移动应用Serverless云服务的平台,Bmob后端云让移动开发更简单,全方位一体化的后端服务平台" name="description"/>
            
            
            
            <link rel="shortcut icon" href="../../img/favicon.ico">
        
        <script type="text/javascript" src="//game.bmob.cn/static/doc_union.js"></script>
            <!--
            <title>Python - Bmob文档中心</title>
            -->
            <title>云函数 &middot;  – Bmob后端云 </title>        
            
            <link href="../../css/bootstrap-custom.min.css" rel="stylesheet">
            <link href="../../css/font-awesome-4.5.0.css" rel="stylesheet">
            <link href="../../css/base.css" rel="stylesheet">
            <link rel="stylesheet" href="../../css/highlight.css">
            <link href="../../css/agate.css" rel="stylesheet">
            <link href="../../css/custom.css" rel="stylesheet" id="custom">
    </head>

    <body >

        <div class="navbar navbar-fixed-top" role="navigation">
    <div class="main-nav">
        <!-- Collapsed navigation -->
        <div class="navbar-header">
            <a class="navbar-brand" href="../..">
                <!--Bmob文档中心-->
                <img src="../../img/logo.png" alt="">
            </a>
        </div>

        <!-- Expanded navigation -->
        <div class="navbar-collapse collapse">

          <ul class="nav navbar-nav">
            
              <li class="pull-left">
                
					<a href="../..">文档首页</a>
                
              </li>
            
              <li class="pull-left">
                
					<!--<a href="">数据服务</a>-->
					<li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">数据服务 <b class="caret"></b></a>
                        <ul class="dropdown-menu">
                            
								<li >
									<a href="../../data/android/">Android</a>
                                </li>
                            
                            
								<li >
									<a href="../../data/ios/">iOS</a>
                                </li>
                            
                            
								<li >
									<a href="../../data/csharp/">C#</a>
                                </li>
                            
                            
								<li >
									<a href="../../data/php/">PHP</a>
                                </li>
                            
                            
								<li >
									<a href="../../data/go/">GO</a>
                                </li>
                            
                            
								<li >
									<a href="../../data/restful/">REST API</a>
                                </li>
                            
                            
								<li >
									<a href="../../data/wechat_app_new/rm/">JavaScript</a>
                                </li>
                            
                            
                                <li >
                                    <a href="../../data/cocos2d_x/">Cocos2D-X</a>
                                </li>
							
                            
								<li >
									<a href="../../data/wechat_app/">小程序</a>
                                </li>
                            
                            
                                <li >
                                    <a href="../../data/wechat_app_new/">快应用</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../data/wechat_app_new/">Nodejs</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../data/wechat_app_new/">Cocos Creator</a>
                                </li>
							
                            
								<li >
									<a href="../../data/wechat_app_new/rm/">小程序(新)</a>
                                </li>
                            
                            
                                <li >
                                    <a href="../../data/kotlin/">Kotlin</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../data/python/">Python</a>
                                </li>
							
                        </ul>
                    </li>
                
              </li>
            
              <li class="pull-left active">
                
					<!--<a href="../android/">云函数</a>-->
					<li class="dropdown active">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">云函数 <b class="caret"></b></a>
                        <ul class="dropdown-menu">
                            
                                <li >
                                    <a href="../android/">Android</a>
                                </li>
							
                            
                                <li >
                                    <a href="../ios/">iOS</a>
                                </li>
							
                            
                                <li >
                                    <a href="../java/">Java</a>
                                </li>
							
                            
                                <li class="active">
                                    <a href="./">Python</a>
                                </li>
							
                            
                                <li >
                                    <a href="../csharp/">C#</a>
                                </li>
							
                            
                                <li >
                                    <a href="../php/">PHP</a>
                                </li>
							
                            
                                <li >
                                    <a href="../javascript/">JavaScript</a>
                                </li>
							
                            
                                <li >
                                    <a href="../restful/">REST API</a>
                                </li>
							
                            
								<li >
									<a href="../web/">Web</a>
                                </li>
                            
                        </ul>
                    </li>
                
              </li>
            
              <li class="pull-left">
                
					<!--<a href="../../sms/android/">短信服务</a>-->
					<li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">短信服务 <b class="caret"></b></a>
                        <ul class="dropdown-menu">
                            
                                <li >
                                    <a href="../../sms/android/">Android</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../sms/ios/">iOS</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../sms/php/">PHP</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../sms/javascript/">JavaScript</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../sms/restful/">REST API</a>
                                </li>
							
                        </ul>
                    </li>
                
              </li>
            
              <li class="pull-left">
                
					<!--<a href="">游戏实时后端</a>-->
					<li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">游戏实时后端 <b class="caret"></b></a>
                        <ul class="dropdown-menu">
                            
								<li >
									<a href="../../game/unity/quick_start/">Unity</a>
                                </li>
                            
                            
								<li >
									<a href="../../game/cocos_creator/quick_start/">Cocos Creator</a>
                                </li>
                            
                            
								<li >
									<a href="../../game/wechat_games/quick_start/">微信小游戏</a>
                                </li>
                            
                            
                                <li >
                                    <a href="../../game/cloud_function/develop_doc/">云函数</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../game/classic_case/">经典案例</a>
                                </li>
							
                        </ul>
                    </li>
                
              </li>
            
              <li class="pull-left">
                
					<!--<a href="">即时通讯</a>-->
					<li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">即时通讯 <b class="caret"></b></a>
                        <ul class="dropdown-menu">
                            
								<li >
									<a href="../../im/android/">Android</a>
                                </li>
                            
                            
								<li >
									<a href="../../im/ios/">iOS</a>
                                </li>
                            
                        </ul>
                    </li>
                
              </li>
            
              <li class="pull-left">
                
					<!--<a href="../../push/android/">推送服务</a>-->
					<li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">推送服务 <b class="caret"></b></a>
                        <ul class="dropdown-menu">
                            
                                <li >
                                    <a href="../../push/android/">Android</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../push/ios/">iOS</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../push/php/">PHP</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../push/javascript/">JavaScript</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../push/restful/">REST API</a>
                                </li>
							
                        </ul>
                    </li>
                
              </li>
            
              <li class="pull-left">
                
					<!--<a href="../../other/common_problem/">其他</a>-->
					<li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">其他 <b class="caret"></b></a>
                        <ul class="dropdown-menu">
                            
                                <li >
                                    <a href="../../other/common_problem/">常见问题</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../other/error_code/">错误码</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../other/data_safety/">数据安全</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../other/bql/">BQL</a>
                                </li>
							
                        </ul>
                    </li>
                
              </li>
            
              <li>
                  <a href="https://www.bmob.cn/repository/index" target="_blank">知识库</a>
              </li>
              <li>
                  <a href="http://doc.bmob.cn/video/index.html" target="_blank">视频教程</a>
              </li>
            </ul>
            
        </div>
    </div>
</div>

        <div class="pagebody" id="main-wrapper">
            <div class="sidebar">
                <div class="bs-sidebar hidden-print affix well" role="complementary">

	
		
	
		
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
		
	
		
				
				
				
				
					<div class="code-title">Python</div>
				
				
				
				
				
				
		
	
		
				
				
				
				
				
		
	
		
				
				
				
				
				
		
	
		
				
				
		
	
		
				
				
				
				
				
		
	
		
				
				
				
				
		
	


	<ul class="nav bs-sidenav">
		
			
		
			
		
			
				
					
						
					
						
					
						
					
						
							
								
									
										<li class="active "><a href="#python">Python云函数</a>
											
											<ul class="nav nav-l2">
												<li><a class="itm-l2" href="#_1">调用方法</a></li>
											</ul>
											
											<ul class="nav nav-l2">
												<li><a class="itm-l2" href="#_3">工具</a></li>
											</ul>
											
											<ul class="nav nav-l2">
												<li><a class="itm-l2" href="#_4">代码规范</a></li>
											</ul>
											
											<ul class="nav nav-l2">
												<li><a class="itm-l2" href="#_5">方法参数</a></li>
											</ul>
											
											<ul class="nav nav-l2">
												<li><a class="itm-l2" href="#_6">内置类</a></li>
											</ul>
											
											<ul class="nav nav-l2">
												<li><a class="itm-l2" href="#_8">示例</a></li>
											</ul>
											
											<ul class="nav nav-l2">
												<li><a class="itm-l2" href="#_9">注意事项</a></li>
											</ul>
											
										</li>
									
								
							
						
					
						
					
						
					
						
					
						
					
						
					
				
			
		
			
		
			
		
			
		
			
		
			
		
	</ul>
</div>
            </div>
            <div class="content" role="main">
                <div class="wrap">
                    

<h1 id="python">Python云函数<a class="headerlink" href="#python" title="Permanent link">&para;</a></h1>
<ul>
<li>云函数是一段部署在服务端的代码片段，采用 java、python或node.js 进行编写，然后部署运行在Bmob服务器</li>
<li>通过云函数可以解决很多复杂的业务逻辑，从此无需将要将大量的数据发送到移动设备上做计算处理</li>
<li>只需将这些计算都交由服务端运算处理，最后移动客户端仅仅需要接收云函数运算处理返回的数据结果就可以了</li>
<li>通过更新云函数代码片段，客户端无需更新，便满足业务改动的需求。这样云函数便有更多的灵活性和自主性</li>
</ul>
<h2 id="_1">调用方法<a class="headerlink" href="#_1" title="Permanent link">&para;</a></h2>
<ul>
<li>前提是需要在 <a href="https://www.bmob.cn">https://www.bmob.cn</a> 注册账号、创建应用</li>
</ul>
<p>Python云函数允许以以下方式调用：</p>
<ul>
<li>Http直接请求</li>
<li>REST API接口</li>
<li>Android SDK</li>
<li>iOS SDK、</li>
<li>微信小程序</li>
<li>微信小游戏</li>
<li>C# SDK</li>
<li>快应用</li>
<li>PHP SDK</li>
<li>JavaScript</li>
<li>...</li>
</ul>
<table>
<thead>
<tr>
<th align="center">调用方式</th>
<th align="center">所需信息</th>
<th align="center">优点</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center">SDK</td>
<td align="center">AppId</td>
<td align="center">交互自带加密,接入快速</td>
</tr>
<tr>
<td align="center">RestApi</td>
<td align="center">AppId、RestKey</td>
<td align="center">所有平台适用，通用性强</td>
</tr>
<tr>
<td align="center">Http请求</td>
<td align="center">Secret Key</td>
<td align="center">所有平台适用，可用浏览器打开</td>
</tr>
</tbody>
</table>
<h3 id="rest-api-api">REST API API<a class="headerlink" href="#rest-api-api" title="Permanent link">&para;</a></h3>
<ol>
<li>
<p>调用 <code>api.bmob.cn</code> ，与调用Java、NodeJS版云函数的方式 <strong>完全相同</strong>。这种方式下，服务器会 <strong>自动判断语言</strong>，但 <strong>限制 <code>Method</code> 为 <code>Post</code> 且 <code>Content-Type</code> 为 <code>application/json</code></strong></p>
</li>
<li>
<p>调用 <code>pycloud.bmob.cn</code> ，调用方式基本相同，这种方式 <strong>仅可调用Python云函数</strong>，但 <strong>不限制 <code>Method</code> 和 <code>Content-Type</code></strong></p>
</li>
</ol>
<pre><code># 使用Appid + RestKey请求api2.bmob.cn域名(自动判断语言)
curl -X POST \
    -H &quot;X-Bmob-Application-Id: Your Application ID&quot; \
    -H &quot;X-Bmob-REST-API-Key: Your REST API Key&quot; \
    -H &quot;Content-Type: application/json&quot; \
    -d '{&quot;name&quot;: &quot;zwr&quot;}' \
    https://api2.bmob.cn/1/functions/[function name]

# 使用Appid + RestKey请求pycloud.bmob.cn域名(仅支持Python云函数)
curl -X [method] \
    -H &quot;X-Bmob-Application-Id: Your Application ID&quot; \
    -H &quot;X-Bmob-REST-API-Key: Your REST API Key&quot; \
    -d '[body]' \
    https://pycloud.bmob.cn/1/functions/[function name]

# 使用Master Key请求
curl -X [method] \
    -H &quot;X-Bmob-Master-Key: Your Master Key&quot; \
    -d '[body]' \
    https://pycloud.bmob.cn/1/functions/[function name]
</code></pre>

<h3 id="http">Http请求<a class="headerlink" href="#http" title="Permanent link">&para;</a></h3>
<pre><code># 使用Secret Key请求
curl -X [method] \
    -H &quot;header key: header value&quot; \
    -d '[body]' \
    https://pycloud.bmob.cn/[sectet key]/[function name]

# 或者直接用浏览器打开，即GET请求
https://pycloud.bmob.cn/[sectet key]/[function name]?k1=v1&amp;k2=v2
</code></pre>

<hr />
<p>以下是Bmob各种SDK调用Python云函数的方法，与调用Java、NodeJS版云函数的方式 <strong>完全相同</strong></p>
<h3 id="android-sdk">Android SDK<a class="headerlink" href="#android-sdk" title="Permanent link">&para;</a></h3>
<pre><code>AsyncCustomEndpoints ace = new AsyncCustomEndpoints();
ace.callEndpoint(Context, String funcName, JSONObject params, new CloudCodeListener() {
    @Override
    public void done(Object object, BmobException e) {
        if (e == null) 
            Log.e(TAG, &quot;Succeed: &quot; + object);
        else
            Log.e(TAG, &quot;Failed: &quot; + e);
    }
});
</code></pre>

<h3 id="_2">微信小程序<a class="headerlink" href="#_2" title="Permanent link">&para;</a></h3>
<pre><code>Bmob.Cloud.run('test', {'name': 'zwr'}).then(function (result) {
    console.log(&quot;Succeed: &quot;);
    console.log(result);
}, function (error) {
    console.log(&quot;Failed: &quot;);
    console.log(error);
});
</code></pre>

<h3 id="ios-sdk">iOS SDK<a class="headerlink" href="#ios-sdk" title="Permanent link">&para;</a></h3>
<pre><code>[BmobCloud callFunctionInBackground:@&quot;funcName&quot; withParameters:nil block:^(id object, NSError *error) {
    if (error) {
      NSLog(@&quot;Failed: %@&quot;,[error description]);
    }else{
        NSLog(@&quot;Succeed: %@&quot;,object);
    }
}] ;
</code></pre>

<h3 id="c-sdk">C# SDK<a class="headerlink" href="#c-sdk" title="Permanent link">&para;</a></h3>
<pre><code>IDictionary&lt;String, Object&gt; parameters ＝  new IDictionary&lt;String, Object&gt;{{&quot;name&quot;,&quot;zwr&quot;}};
Bmob.Endpoint&lt;Hashtable&gt;(&quot;test&quot;, parameters, (resp, exception) =&gt; 
{
    if (exception == null)
    {
        print(&quot;Succeed: &quot; + resp);
    }
    else
    {
        print(&quot;Failed: &quot; + exception.Message);
    }
});
</code></pre>

<h3 id="php-sdk">PHP SDK<a class="headerlink" href="#php-sdk" title="Permanent link">&para;</a></h3>
<pre><code>$cloudCode = new BmobCloudCode('test'); //调用名字为test的云函数
$res = $cloudCode-&gt;get(array(&quot;name&quot;=&gt;&quot;zwr&quot;)); //传入参数name，其值为zwr
</code></pre>

<h3 id="javascript">JavaScript<a class="headerlink" href="#javascript" title="Permanent link">&para;</a></h3>
<pre><code>Bmob.Cloud.run('test', {&quot;name&quot;:&quot;tom&quot;}, {
    success: function(result) {
        console.log(&quot;Succeed: &quot;);
        console.log(result);
    },
    error: function(error) {
        console.log(&quot;Failed: &quot;);
        console.log(error);
    }
});
</code></pre>

<h2 id="_3">工具<a class="headerlink" href="#_3" title="Permanent link">&para;</a></h2>
<p>Github页面如下：</p>
<p><a href="https://github.com/bmob/CloudFunction/tree/master/python/">https://github.com/bmob/CloudFunction/tree/master/python/</a> </p>
<ul>
<li><code>exec目录</code> 下提供了 <code>macos</code>、<code>linux</code>、<code>windows 64位</code>等平台的<code>可执行文件</code>，以供开发者快速进行代码的上传、修改、同步到本地和删除</li>
<li><code>samples目录</code> 提供了案例</li>
</ul>
<h2 id="_4">代码规范<a class="headerlink" href="#_4" title="Permanent link">&para;</a></h2>
<ul>
<li>python云函数必须遵循以下格式：</li>
</ul>
<pre><code>def onRequest(request, response, modules):
    # 上面这个方法声明，不允许任何修改
    # 这里使用Python编写云函数
</code></pre>

<ul>
<li>代码不能包含以下关键字：(保存代码时有错误提醒)</li>
</ul>
<pre><code>import
exit
eval
...
</code></pre>

<ul>
<li>如果在业务场景中需要用到被禁止使用的关键字，例如查询"import"表，可用"imp"+"ort"的形式拼接出来</li>
<li>如果除了内置模块(见下方)之外有需要使用的原生模块或著名模块，请提交工单申请开通</li>
<li>云函数执行完毕后，必须用response.send方法返回响应数据，否则会被当做超时，多次超时可能会被暂停使用</li>
</ul>
<h2 id="_5">方法参数<a class="headerlink" href="#_5" title="Permanent link">&para;</a></h2>
<h3 id="request">request对象<a class="headerlink" href="#request" title="Permanent link">&para;</a></h3>
<p>onRequest方法参数中 <code>request</code> 包含了本次请求的全部信息：</p>
<table>
<thead>
<tr>
<th align="center">名称</th>
<th align="center">类型</th>
<th align="center">获取方法</th>
<th align="center">示例</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center">路径Path</td>
<td align="center">str</td>
<td align="center">request.getPath()</td>
<td align="center">/xxxxxxxxxxxxxxxx/test1</td>
</tr>
<tr>
<td align="center">方法Method</td>
<td align="center">str</td>
<td align="center">request.getMethod()</td>
<td align="center">POST</td>
</tr>
<tr>
<td align="center">请求头Headers</td>
<td align="center">dict</td>
<td align="center">request.getHeaders()</td>
<td align="center">{"User-Agent":["Chorme"]}</td>
</tr>
<tr>
<td align="center">请求体Body</td>
<td align="center">str</td>
<td align="center">request.getBody()</td>
<td align="center">[98, 109, 111, 98]</td>
</tr>
<tr>
<td align="center">Get参数</td>
<td align="center">dict</td>
<td align="center">request.getQueryParams()</td>
<td align="center">{"page": "1"}</td>
</tr>
<tr>
<td align="center">Body内参数</td>
<td align="center">dict</td>
<td align="center">request.getParams()</td>
<td align="center">{"username": "zwr"}</td>
</tr>
<tr>
<td align="center">单个请求头</td>
<td align="center">str</td>
<td align="center">request.getHeader(String key)</td>
<td align="center">request.getHeader("User-Agent") = "Chrome"</td>
</tr>
<tr>
<td align="center">单个Get参数</td>
<td align="center">str</td>
<td align="center">request.getQueryParam(String key)</td>
<td align="center">request.getQueryParam("page") = "1"</td>
</tr>
</tbody>
</table>
<h3 id="response">response对象<a class="headerlink" href="#response" title="Permanent link">&para;</a></h3>
<ul>
<li>onRequest方法参数中 <code>response</code> 用于响应请求，返回数据</li>
<li>Response对象仅有名为 <code>send</code> 的方法</li>
</ul>
<pre><code>response.send(res = &quot;&quot;, statusCode = 200, statusMsg = &quot;OK&quot;, headers = None)
</code></pre>

<p>以下是参数说明：</p>
<table>
<thead>
<tr>
<th align="center">名称</th>
<th align="center">类型</th>
<th align="center">意义</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center">res</td>
<td align="center">object</td>
<td align="center">返回的内容</td>
</tr>
<tr>
<td align="center">statusCode</td>
<td align="center">int</td>
<td align="center">返回的Http响应状态码，例如200、404</td>
</tr>
<tr>
<td align="center">statusMsg</td>
<td align="center">str</td>
<td align="center">返回的Http响应状态，例如OK、NotFound</td>
</tr>
<tr>
<td align="center">headers</td>
<td align="center">dict</td>
<td align="center">返回的头部信息，例如{"Content-Type": "text/plain; charset=UTF-8"}</td>
</tr>
</tbody>
</table>
<ul>
<li>示例</li>
</ul>
<pre><code># 1. 直接返回字符串
response.send(&quot;Hello world--Bmob&quot;);
# 2. 返回404错误
response.send(&quot;Error&quot;, 404, &quot;NotFound&quot;);
# 3. 返回中文字符串，需要返回包含charset的header
response.send(
    &quot;你好，比目&quot;,
    200,
    &quot;OK&quot;,
    {
        &quot;Content-Type&quot;: &quot;text/plain; charset=UTF-8&quot;
    }
);
</code></pre>

<h3 id="modules">modules对象<a class="headerlink" href="#modules" title="Permanent link">&para;</a></h3>
<ul>
<li>onRequest方法参数中 <code>modules</code> 提供第三方模块供开发者调用：</li>
</ul>
<table>
<thead>
<tr>
<th align="center">模块名</th>
<th align="center">获取方式</th>
<th align="center">作用</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center">Bmob数据库操作</td>
<td align="center">modules.oData</td>
<td align="center">封装了Bmob的大多数api，以供开发者进行快速的业务逻辑开发</td>
</tr>
</tbody>
</table>
<h4 id="bmob">Bmob数据操作<a class="headerlink" href="#bmob" title="Permanent link">&para;</a></h4>
<p>以下均为 <code>modules.oData</code> 的方法：</p>
<table>
<thead>
<tr>
<th align="center">方法体</th>
<th align="center">返回值</th>
<th align="center">描述</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center">setUserSession(session)</td>
<td align="center">self</td>
<td align="center">设置用户的Session Token</td>
</tr>
<tr>
<td align="center">setMasterKey(masterKey)</td>
<td align="center">self</td>
<td align="center">设置应用的Master Key</td>
</tr>
<tr>
<td align="center">insert(className, data)</td>
<td align="center">HttpResponse</td>
<td align="center">往数据表中添加一行</td>
</tr>
<tr>
<td align="center">remove(className, objectId)</td>
<td align="center">HttpResponse</td>
<td align="center">删除数据表中的一行</td>
</tr>
<tr>
<td align="center">update(className, objectId, data)</td>
<td align="center">HttpResponse</td>
<td align="center">更新数据表中的一行</td>
</tr>
<tr>
<td align="center">find</td>
<td align="center">HttpResponse</td>
<td align="center">使用查询器查询数据，详细参数见下方</td>
</tr>
<tr>
<td align="center">findOne(className, objectId)</td>
<td align="center">HttpResponse</td>
<td align="center">查询数据表中的一行</td>
</tr>
<tr>
<td align="center">userSignUp(userData)</td>
<td align="center">HttpResponse</td>
<td align="center">用户注册</td>
</tr>
<tr>
<td align="center">userLogin(username, password)</td>
<td align="center">HttpResponse</td>
<td align="center">用户通过账号、密码登陆</td>
</tr>
<tr>
<td align="center">userLoginBySMS(mobile, smsCode, userInfo)</td>
<td align="center">HttpResponse</td>
<td align="center">用户通过短信验证码一键注册或登录</td>
</tr>
<tr>
<td align="center">userResetPasswordByEmail(email)</td>
<td align="center">HttpResponse</td>
<td align="center">用户请求Email重置密码</td>
</tr>
<tr>
<td align="center">userResetPasswordBySMS(smsCode, password)</td>
<td align="center">HttpResponse</td>
<td align="center">用户通过短信验证码重置密码</td>
</tr>
<tr>
<td align="center">userResetPasswordByPWD(userId,session,oldPassword,newPassword)</td>
<td align="center">HttpResponse</td>
<td align="center">用户通过旧密码修改新密码</td>
</tr>
<tr>
<td align="center">sendCustomSMS(mobile, content)</td>
<td align="center">HttpResponse</td>
<td align="center">发送自定义短信</td>
</tr>
<tr>
<td align="center">sendSMSCode(mobile, template)</td>
<td align="center">HttpResponse</td>
<td align="center">发送某模版的短信验证码</td>
</tr>
<tr>
<td align="center">verifySMSCode(mobile, smsCode)</td>
<td align="center">HttpResponse</td>
<td align="center">验证短信验证码</td>
</tr>
<tr>
<td align="center">payQuery(orderId)</td>
<td align="center">HttpResponse</td>
<td align="center">查询支付订单</td>
</tr>
<tr>
<td align="center">cloudCode(funcName, body = None)</td>
<td align="center">HttpResponse</td>
<td align="center">调用云函数</td>
</tr>
<tr>
<td align="center">getDBTime()</td>
<td align="center">获取REST API服务器的时间</td>
<td align="center"></td>
</tr>
<tr>
<td align="center">batch(requests, isTransaction = None)</td>
<td align="center">HttpResponse</td>
<td align="center">批量请求</td>
</tr>
</tbody>
</table>
<hr />
<p><strong>查询方法</strong> ：</p>
<pre><code>modules.oData.find(
    table,
    where = None, # 设置查询条件, dict或BmobQuerier
    limit = None, # 设置最大返回行数，int
    skip = None, # 设置跳过的个数，int
    order = None, # 排序规则，str
    include = None, # 需要返回详细信息的Pointer属性，str
    keys = None, # 需要返回的属性，str
    count = None, # 统计接口: 返回数量，int
    groupby = None, # 统计接口: 根据某列分组，str
    groupcount = None, # 统计接口: 分组后组内统计数量，bool
    min = None, # 统计接口: 获取最小值，str
    max = None, # 统计接口: 获取最大值，str
    sum = None, # 统计接口: 计算总数，str
    average = None, # 统计接口: 计算平均数，str
    having = None, # 统计接口: 分组中的过滤条件，str
    objectId = None # 查询单条数据，str
)
</code></pre>

<h2 id="_6">内置类<a class="headerlink" href="#_6" title="Permanent link">&para;</a></h2>
<h3 id="httpresponse">HttpResponse<a class="headerlink" href="#httpresponse" title="Permanent link">&para;</a></h3>
<p><strong>类变量</strong>:</p>
<table>
<thead>
<tr>
<th align="center">变量名</th>
<th align="center">类型</th>
<th align="center">描述</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center">code</td>
<td align="center">int</td>
<td align="center">状态码</td>
</tr>
<tr>
<td align="center">status</td>
<td align="center">str</td>
<td align="center">状态信息</td>
</tr>
<tr>
<td align="center">headers</td>
<td align="center">dict</td>
<td align="center">返回的头部</td>
</tr>
<tr>
<td align="center">stringData</td>
<td align="center">str</td>
<td align="center">返回的数据</td>
</tr>
<tr>
<td align="center">jsonData</td>
<td align="center">dict</td>
<td align="center">返回的json数据</td>
</tr>
<tr>
<td align="center">queryResults</td>
<td align="center">dict</td>
<td align="center">返回的bmob查询数据</td>
</tr>
<tr>
<td align="center">statCount</td>
<td align="center">int</td>
<td align="center">返回的bmob统计数据</td>
</tr>
<tr>
<td align="center">err</td>
<td align="center">String</td>
<td align="center">错误信息</td>
</tr>
</tbody>
</table>
<p><strong>类方法</strong></p>
<table>
<thead>
<tr>
<th align="center">方法名</th>
<th align="center">返回类型</th>
<th align="center">描述</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center">updatedAt</td>
<td align="center">str或None</td>
<td align="center">bmob操作更新后的返回值</td>
</tr>
<tr>
<td align="center">createdAt</td>
<td align="center">str或None</td>
<td align="center">bmob操作新增后的返回值</td>
</tr>
<tr>
<td align="center">objectId</td>
<td align="center">str或None</td>
<td align="center">bmob操作新增后的返回值</td>
</tr>
<tr>
<td align="center">msg</td>
<td align="center">str或None</td>
<td align="center">bmob操作删除、验证短信后的返回值</td>
</tr>
</tbody>
</table>
<h3 id="bmobquerier">BmobQuerier<a class="headerlink" href="#bmobquerier" title="Permanent link">&para;</a></h3>
<p><strong>类方法</strong>: <strong>返回类型均为 <code>BmobQuerier</code></strong> (以链式调用)</p>
<table>
<thead>
<tr>
<th align="center">方法体</th>
<th align="center">描述</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center">addWhereExists(key)</td>
<td align="center">某字段有值</td>
</tr>
<tr>
<td align="center">addWhereNotExists(key)</td>
<td align="center">某字段无值</td>
</tr>
<tr>
<td align="center">addWhereEqualTo(key, value)</td>
<td align="center">某字段等于</td>
</tr>
<tr>
<td align="center">addWhereNotEqualTo(key, value)</td>
<td align="center">某字段不等于</td>
</tr>
<tr>
<td align="center">addWhereGreaterThan(key, value)</td>
<td align="center">某字段大于</td>
</tr>
<tr>
<td align="center">addWhereGreaterThanOrEqualTo(key, value)</td>
<td align="center">某字段大于等于</td>
</tr>
<tr>
<td align="center">addWhereLessThan(key, value)</td>
<td align="center">某字段小于</td>
</tr>
<tr>
<td align="center">addWhereLessThanOrEqualTo(key, value)</td>
<td align="center">某字段小于等于</td>
</tr>
<tr>
<td align="center">addWhereRelatedTo(table,toObjId,toKey)</td>
<td align="center">在某表作为Relation关联起来的数据</td>
</tr>
<tr>
<td align="center">addWhereNear(key,bmobGeoPoint,maxMiles,maxKM,maxRadians)</td>
<td align="center">地理位置在一定范围内</td>
</tr>
<tr>
<td align="center">addWhereWithinGeoBox(key,southwest,northeast)</td>
<td align="center">地理位置在矩形范围内</td>
</tr>
<tr>
<td align="center">addWhereContainedIn(key,objs)</td>
<td align="center">值在列表内</td>
</tr>
<tr>
<td align="center">addWhereNotContainedIn(key,objs)</td>
<td align="center">值不在列表内</td>
</tr>
<tr>
<td align="center">addWhereContainsAll(key,objs)</td>
<td align="center">列表包含全部项</td>
</tr>
<tr>
<td align="center">addWhereStrContains(key,regex)</td>
<td align="center">String类型模糊查询</td>
</tr>
<tr>
<td align="center">addWhereMatchesSelect(key,innerQuery,innerKey,innerTable,isMatch)</td>
<td align="center">某项符合子查询</td>
</tr>
<tr>
<td align="center">addWhereInQuery(key,value,className,isIn)</td>
<td align="center">某项包含在子查询</td>
</tr>
</tbody>
</table>
<h3 id="bmobupdater">BmobUpdater<a class="headerlink" href="#bmobupdater" title="Permanent link">&para;</a></h3>
<p>该类的全部静态方法都用于设置insert、update方法的请求内容，<strong>返回类型均为 <code>dict</code></strong></p>
<p><strong>静态方法</strong>：</p>
<table>
<thead>
<tr>
<th align="center">方法体</th>
<th align="center">描述</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center">add(key,value,data=None)</td>
<td align="center">往data添加一个键值</td>
</tr>
<tr>
<td align="center">increment(key,number,data)</td>
<td align="center">原子计数</td>
</tr>
<tr>
<td align="center">arrayAdd(key,value,data)</td>
<td align="center">往Array类型添加项</td>
</tr>
<tr>
<td align="center">arrayAddUnique(key,value,data)</td>
<td align="center">往Array类型不重复地添加项</td>
</tr>
<tr>
<td align="center">arrayRemove(key,value,data)</td>
<td align="center">删除Array类型的多项</td>
</tr>
<tr>
<td align="center">addRelations(key,value,data)</td>
<td align="center">添加多个Relation关系</td>
</tr>
<tr>
<td align="center">removeRelations(key,value,data)</td>
<td align="center">移除多个Relation关系</td>
</tr>
</tbody>
</table>
<h3 id="bmobpointer">BmobPointer<a class="headerlink" href="#bmobpointer" title="Permanent link">&para;</a></h3>
<p>构造方法：</p>
<pre><code>BmobPointer(className, objectId)
</code></pre>

<h3 id="bmobfile">BmobFile<a class="headerlink" href="#bmobfile" title="Permanent link">&para;</a></h3>
<p>构造方法：</p>
<pre><code>BmobFile(url, filename=&quot;&quot;)
</code></pre>

<h3 id="bmobdate">BmobDate<a class="headerlink" href="#bmobdate" title="Permanent link">&para;</a></h3>
<p>构造方法：</p>
<pre><code>BmobDate(timeStamp) # 毫秒
BmobDate(dateStr)
</code></pre>

<h3 id="bmobgeopoint">BmobGeoPoint<a class="headerlink" href="#bmobgeopoint" title="Permanent link">&para;</a></h3>
<p>构造方法：</p>
<pre><code>BmobGeoPoint(longitude, latitude)
</code></pre>

<h3 id="_7">原生模块<a class="headerlink" href="#_7" title="Permanent link">&para;</a></h3>
<h4 id="json">json<a class="headerlink" href="#json" title="Permanent link">&para;</a></h4>
<ul>
<li>json.dumps</li>
<li>json.loads</li>
<li>...</li>
</ul>
<h4 id="math">math<a class="headerlink" href="#math" title="Permanent link">&para;</a></h4>
<ul>
<li>math.log</li>
<li>math.sin</li>
<li>...</li>
</ul>
<h4 id="base64">base64<a class="headerlink" href="#base64" title="Permanent link">&para;</a></h4>
<ul>
<li>base64.b64decode</li>
<li>base64.b64encode</li>
<li>...</li>
</ul>
<h4 id="re">re<a class="headerlink" href="#re" title="Permanent link">&para;</a></h4>
<ul>
<li>re.findall</li>
<li>re.split</li>
<li>...</li>
</ul>
<h4 id="time">time<a class="headerlink" href="#time" title="Permanent link">&para;</a></h4>
<ul>
<li>time.mktime</li>
<li>time.strftime</li>
<li>time.strptime</li>
<li>...</li>
</ul>
<h2 id="_8">示例<a class="headerlink" href="#_8" title="Permanent link">&para;</a></h2>
<p>案例主要放在了Github: <strong><a href="https://github.com/bmob/CloudFunction/tree/master/python/samples">BmobPython云函数案例</a></strong></p>
<h2 id="_9">注意事项<a class="headerlink" href="#_9" title="Permanent link">&para;</a></h2>
<ul>
<li>
<p>如果你编写的Python云函数经常发生运行超时、上下行超流量、滥用内存等现象，官方将会自动封停你的云函数功能，修改后向客服申请方可继续使用</p>
</li>
<li>
<p>如果某接口调用频率较高，超过默认并发量，则会直接返回错误，解决方法：</p>
</li>
</ul>
<pre><code>1. 修改客户端代码，降低请求频率
2. 修改云函数，提高代码质量和效率，减少网络请求相关的超时时长，尽快结束工作
3. 联系客服单独配置机器，随你怎么折腾
</code></pre>

<ul>
<li>如果需要接受更大的请求体，或返回更大的结果，请购买更高的配置</li>
<li>如果你不习惯在Web端编辑代码，使用 <a href="https://github.com/bmob/CloudFunction/tree/master/python/exec">同步工具</a> 是一个不错的选择</li>
<li></li>
</ul>
                </div>
            </div>
        </div>

        
            <script>var base_url = '../..';</script>
            <script src="../../js/jquery-1.10.2.min.js"></script>
            <script src="../../js/bootstrap-3.0.3.min.js"></script>
            <script src="../../js/highlight.pack.js"></script>
            <script src="../../js/main.js"></script>
            <script src="../../js/base.js"></script>

        <div id="go-top"><i class="fa fa-chevron-up"></i></div>
        <a href="https://docs.bmob.cn/data/Android/a_faststart/doc/index.html" id="back" target="_blank">返回 <br> 旧版</a>
    </body>

</html>